import { ItemID, EventName, GameText, InitValue } from "../../public/Config";
import User from "../db/User";
import GlobalEvent from "../../cfw/event/GlobalEvent";
import { ResultCallback, ResultState } from "../../sdk/SDKConfig";
import SDKManager from "../../sdk/SDKManager";
import TipController from "../tip/TipController";

export default class BagManager {



    static costItem(id: number, num: number, callback: ResultCallback) {
        switch (id) {
            case ItemID.COIN:
                User.instance().updateCoin(-num)
                callback(ResultState.YES)
                break;
            case ItemID.AD:
                SDKManager.getChannel().showRewardAd(0, (state) => {
                    callback(state)
                });
                break;
        }
    }


    static addCoin(num: number, pos?: cc.Vec2,time:number = 0.5) {
        this.updateNum(ItemID.COIN, num)
        if (pos) {
            GlobalEvent.instance().emit(EventName.ADD_COIN, pos,time)
        }

    }


    private static updateNum(id: number, num: number) {
        switch (id) {
            case ItemID.COIN:
                User.instance().updateCoin(num)
                break;
        }
    }

    static isEnough(id: number, num: number) {
        switch (id) {
            case ItemID.COIN:
                let flag = User.instance().getCoin() >= num;
                if (!flag) {
                    // TipController.instance().showToast(GameText.NO_GOLD)
                    this.buyCoin()
                }
                return flag
            case ItemID.AD:
                return true;
        }
    }

    static buyCoin(callback?:Function) {
        TipController.instance().showTip(GameText.BUY_GOLD, (state: ResultState) => {
            if (state == ResultState.YES) {
                SDKManager.getChannel().showRewardAd(0, (s: ResultState) => {
                    if (s == ResultState.YES) {
                        this.addCoin(InitValue.AD_BUY_GOLD)
                    }
                })
            }
            if(callback){
                callback()
            }
        })
    }

}
